package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.mapper.AlarulesMapper;
import com.j283_5.wisdom_park.pojo.Alarules;
import com.j283_5.wisdom_park.service.AlarulesService;
import com.j283_5.wisdom_park.vo.confcenter_management.AlarulesVo;
import com.j283_5.wisdom_park.vo.confcenter_management.RoleinfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class AlarulesServiceImpl extends ServiceImpl<AlarulesMapper, Alarules> implements AlarulesService {
    @Resource
    private AlarulesMapper alarulesMapper;

    @Override
    public ResultData alaRulesInfo(Page<Alarules> page, Alarules alarules) {
        if (page==null){
            page = new Page<>(1,10);
        }
        QueryWrapper<Alarules> alarulesQueryWrapper=new QueryWrapper<>();
        alarulesQueryWrapper.eq("al_state",0);
        alarulesQueryWrapper.orderByDesc("al_createtime");
        if (alarules.getAlName()!=null){
            alarulesQueryWrapper.like("al_name",alarules.getAlName());
        }
        if (alarules.getAlMode()!=null){
            alarulesQueryWrapper.eq("al_mode",alarules.getAlMode());
        }
        Page<Alarules> alarulesPage=alarulesMapper.selectPage(page,alarulesQueryWrapper);
        if(alarulesPage != null){
            return ResultData.ok().data(alarulesPage);
        }else{
            return ResultData.error().message("查询失败!");
        }
    }

    @Override
    public ResultData addAlaRules(AlarulesVo alarulesVo) {
        Alarules alarules=new Alarules();
        BeanUtils.copyProperties(alarulesVo,alarules);
        int i = alarulesMapper.insert(alarules);
        if (i>0){
            return ResultData.ok().message("添加成功！");
        }
        return ResultData.error().message("添加失败!");
    }

    @Override
    public ResultData deleteAlaRules(ArrayList<Integer> alaRuleslist) {
        UpdateWrapper<Alarules> alarulesUpdateWrapper=new UpdateWrapper<>();
        alarulesUpdateWrapper.set("al_state",1);
        alarulesUpdateWrapper.in("al_id",alaRuleslist);
        int i = alarulesMapper.update(null,alarulesUpdateWrapper);
        if (i>0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.ok().message("删除失败!");
    }

    @Override
    public ResultData updateAlaRules(Alarules alarules) {
        UpdateWrapper<Alarules> alarulesUpdateWrapper=new UpdateWrapper<>();
        alarulesUpdateWrapper.eq("al_id",alarules.getAlId())
                .set("al_name",alarules.getAlName())
                .set("al_type",alarules.getAlType())
                .set("al_mode",alarules.getAlMode())
                .set("al_rules",alarules.getAlRules());
        int i = alarulesMapper.update(null,alarulesUpdateWrapper);
        if (i>0){
            return ResultData.ok().message("修改成功!");
        }
        return ResultData.ok().message("修改失败!");
    }
}
